package com.prs.crm.service.hr.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.courser.EncryptUtils;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.AbstractDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.dao.hr.PersonDao;
import com.prs.crm.domain.hr.Check;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.JobTitle;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Role;
import com.prs.crm.domain.sys.Module;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class PersonServiceImpl extends BaseServiceImpl<Person, PersonDao>
		implements PersonService {

	public boolean login(String usrname, String password) {

		Person person = this.getDao().findByCode(usrname);
		if (person != null) {
			if (person.getRole() != null) {
				for (@SuppressWarnings("unused")
				Module module : person.getRole().getModules()) {

				}
			}
			// if (person.getLoginPwd().equals(EncryptUtils.MD5(password)))

			// if (EncryptUtils.MD5(person.getLoginPwd()).equals(password))
			if ((person.getLoginPwd()).equals(password)) {
				AppContext.createAccountContext(person);
				return true;
			}
		}
		return false;
	}

	public Collection<Person> findByDept(Dept dept) {
		return this.getDao().findByDept(dept);
	}

	public Collection<Person> getDeptRolePersons(Dept dept, Role role) {
		return this.getDao().findByDeptRole(dept, role);
	}

	public Collection<Person> findPersons(Collection<Integer> ids) {
		return this.getDao().findPersons(ids);
	}

	public Collection<Person> findAll() {
		return this.getDao().findAll();
	}

	public Person findByUserName(String username, boolean fuzzy) {
		return (fuzzy) ? this.getDao().fuzzyFindByUserName(username) : this
				.getDao().findByUserName(username);
	}

	public Person findNext(Person person) {
		return this.getDao().findNext(person);
	}

	public Person findPrev(Person person) {
		return this.getDao().findPrev(person);
	}

	public Person findFirstPerson() {
		return this.getDao().findFirstPerson();
	}

	@Override
	public Collection<?> getReportPersonRoleSource() {
		// TODO Auto-generated method stub
		return this.getDao().getReportPersonRoleSource();
	}

	@SuppressWarnings("unchecked")
	@Override
	public AbstractDataset fillDataset(Collection<?> dataList, int GraphicsType) {

		AbstractDataset Dataset = null;
		if (GraphicsType == Constrains.REPORT_PIECHART)
			Dataset = new DefaultPieDataset();
		else if (GraphicsType == Constrains.REPORT_BARCHART)
			Dataset = new DefaultCategoryDataset();

		for (Object[] o : (Collection<Object[]>) dataList) {
			String name = "";
			Number count = 0;
			for (Object value : o) {
				if (value != null) {
					if (value instanceof Role) {
						name = ((Role) value).getName();
					} else if (value instanceof Dept) {
						name = ((Dept) value).getName();
					} else if (value instanceof JobTitle) {
						name = ((JobTitle) value).getName();
					} else if (value instanceof Number) {
						count = (Number) value;
					}
				}
			}
			if (!name.equals("")) {
				if (GraphicsType == Constrains.REPORT_PIECHART)
					((DefaultPieDataset) Dataset).setValue(name, count);
				else if (GraphicsType == Constrains.REPORT_BARCHART)
					((DefaultCategoryDataset) Dataset).addValue(count, name,
							name);
			}
		}
		return Dataset;
	}

	@Override
	public Collection<?> getReportPersonDeptSource() {
		// TODO Auto-generated method stub
		return this.getDao().getReportPersonDeptSource();
	}

	@Override
	public Collection<?> getReportPersonJobTitleSource() {
		// TODO Auto-generated method stub
		return this.getDao().getReportPersonJobTitleSource();
	}

	@Override
	public Collection<Person> getAllPerson() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection<Dept> convertToDept(String ids) {
		// TODO Auto-generated method stub
		try {
			Collection<Dept> depts = new ArrayList<Dept>();
			for (String id : ids.split(",")) {
				Dept dept = new Dept();
				dept.setId(Integer.parseInt(id));
				System.out.println(dept);
				depts.add(dept);
			}
			return depts;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<Person> getAllPersons() {
		// TODO Auto-generated method stub
		return this.getDao().getAllPersons();
	}

	@Override
	public Collection<Check> convertToCheck(String ids) {
		// TODO Auto-generated method stub
		try {
			Collection<Check> checks = new ArrayList<Check>();
			for (String id : ids.split(",")) {
				Check check = new Check();
				check.setId(Integer.parseInt(id));
				checks.add(check);
			}
			return checks;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void removePerson(Person p) {
		this.getDao().removePerson(p);
	}

	@Override
	public List<Person> getNoticePerson(Role role) {
		// TODO Auto-generated method stub
		return this.getDao().getNoticePerson(role);
	}

	@Override
	public Person getByroot(String name) {
		// TODO Auto-generated method stub
		return this.getDao().getByroot(name);
	}

	@Override
	public void saveOrUpdate(Person person) {
		// TODO Auto-generated method stub
		this.getDao().saveOrUpdate(person);
	}

	@Override
	public Person getPersonByMovie(Integer movieId, String userStyle) {
		// TODO Auto-generated method stub
		return this.getDao().getPersonByMovie(movieId, userStyle);
	}

	@Override
	public Person getPersonByTicketer(Integer ticketerId, String userStyle) {
		// TODO Auto-generated method stub
		return this.getDao().getPersonByTicketer(ticketerId, userStyle);
	}

	@Override
	public Integer delByPerson() {
		// TODO Auto-generated method stub
		return this.getDao().delByPerson();
	}

	@Override
	public Person getPersonById(Integer id) {
		return this.getDao().getPersonById(id);
	}

	@Override
	public Collection<Person> getRootPersonBySubordinate() {
		return this.getDao().getRootPersonBySubordinate();
	}

	@Override
	public void saveCenterTable(Person person) throws SQLException {
		this.getDao().saveCenterTable(person);
	}
}
